/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel.layouts;

import java.util.Iterator;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.swing.components.panel.LayoutBuilder;

/**
 * Controí o layout para as ações da transação.
 * @author Glauco
 */
public class TransactionActionLayoutBuilder implements LayoutBuilder{

    JButton searchButton ;
    JButton createButton ;
    JButton updateButton ;
    JButton deleteButton ;
    List<JButton> components;

    public List<JButton> getComponents() {
        return components;
    }

    public void setComponents(List<JButton> components) {
        this.components = components;
    }

    public JButton getCreateButton() {
        return createButton;
    }

    public void setCreateButton(JButton createButton) {
        this.createButton = createButton;
    }

    public JButton getDeleteButton() {
        return deleteButton;
    }

    public void setDeleteButton(JButton deleteButton) {
        this.deleteButton = deleteButton;
    }

    public JButton getSearchButton() {
        return searchButton;
    }

    public void setSearchButton(JButton searchButton) {
        this.searchButton = searchButton;
    }

    public JButton getUpdateButton() {
        return updateButton;
    }

    public void setUpdateButton(JButton updateButton) {
        this.updateButton = updateButton;
    }

    /**
     * Controí o layout para as ações da transação.<br>
     * Deve haver ao menos quatro (4) componentes na lista, que são as quatro
     * ações padrão da transação (busca,novo,editor,excluir).<br>
     * Os outros componentes serão colocado na parte inferior do painel.
     *
     * @param panel Painel das Ações da Transação.
     * @param components Botões das ações da transação.
     * @throws BuilderException
     */
    @Override
    public void buildLayout(JPanel panel) {

        // cria leiaute
        panel.removeAll();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        // HORIZONTAL
        GroupLayout.ParallelGroup hGroup =
                layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                .addComponent(searchButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(createButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(updateButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(deleteButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);

        if (components.size() > 0)
            for (JComponent c : components) {
                hGroup.addComponent(c,
                        GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE);
            }

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(hGroup)
                .addContainerGap());

        // VERTICAL
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(createButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 10, Short.MAX_VALUE);

        if (components.size() > 0){
            Iterator<JButton> iter = components.iterator();
            vGroup.addComponent(iter.next());
            while(iter.hasNext()){
                vGroup.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(iter.next());
            }
            vGroup.addContainerGap();
        }

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
            .addGroup(vGroup));
    }

}
